{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# SkyServe Gorilla Playground\n",
    "\n",
    "Welcome! Here is the sky serve gorilla playground. You can use this notebook to test your gorilla service.\n",
    "\n",
    "This notebook is borrowed from [gorilla's colab](https://colab.research.google.com/drive/1DEBPsccVLF_aUnmD0FwPeHFrtdC0QIUP?usp=sharing).\n",
    "\n",
    "To use this notebook, run `sky serve up examples/serve/gorilla/gorilla.yaml` first and paste the endpoint below."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sky_serve_endpoint = '' # Enter your sky serve endpoint here"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Then run the cell below to test your service!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install openai &> /dev/null\n",
    "import openai\n",
    "import urllib.parse\n",
    "\n",
    "openai.api_key = \"EMPTY\" # Key is ignored and does not matter\n",
    "openai.api_base = f\"http://{sky_serve_endpoint}/v1\"\n",
    "\n",
    "# Report issues\n",
    "def raise_issue(e, model, prompt):\n",
    "    issue_title = urllib.parse.quote(\"[bug] Hosted Gorilla: <Issue>\")\n",
    "    issue_body = urllib.parse.quote(f\"Exception: {e}\\nFailed model: {model}, for prompt: {prompt}\")\n",
    "    issue_url = f\"https://github.com/ShishirPatil/gorilla/issues/new?assignees=&labels=hosted-gorilla&projects=&template=hosted-gorilla-.md&title={issue_title}&body={issue_body}\"\n",
    "    print(f\"An exception has occurred: {e} \\nPlease raise an issue here: {issue_url}\")\n",
    "\n",
    "# Query Gorilla server\n",
    "def get_gorilla_response(prompt=\"I would like to translate from English to French.\", model=\"gorilla-falcon-7b-hf-v0\"):\n",
    "  try:\n",
    "    completion = openai.ChatCompletion.create(\n",
    "      model=model,\n",
    "      messages=[{\"role\": \"user\", \"content\": prompt}]\n",
    "    )\n",
    "    return completion.choices[0].message.content\n",
    "  except Exception as e:\n",
    "    raise_issue(e, model, prompt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Gorilla `gorilla-mpt-7b-hf-v1` with code snippets\n",
    "# Translation\n",
    "prompt = \"I would like to translate 'I feel very good today.' from English to Chinese.\"\n",
    "print(get_gorilla_response(prompt, model=\"gorilla-falcon-7b-hf-v0\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Gorilla `gorilla-7b-hf-v1` with code snippets\n",
    "# Object Detection\n",
    "prompt = \"I want to build a robot that can detecting objects in an image ‘cat.jpeg’. Input: [‘cat.jpeg’]\"\n",
    "print(get_gorilla_response(prompt, model=\"gorilla-falcon-7b-hf-v0\"))"
   ]
  }
 ],
 "nbformat": 4,
 "nbformat_minor": 2
}
